﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MentorBackEnd.Model;
using System.Data.SqlClient;
using System.Data;

namespace MentorBackEnd.DA
{
    public  class MatchDA
    {
        public static List<MemaUser> MatchMentors(int userID)
        {
            SqlConnection conn = DataAccessor.GetConnection();
            IDataReader reader = null;
            List<MemaUser> collection = new List<MemaUser>();
            

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_MatchMentorMentee]");
                cmd.Parameters.Add(new SqlParameter("@MenteeID", userID));
                cmd.ExecuteNonQuery();

                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    MemaUser dataModel = new MemaUser(reader);
                    collection.Add(dataModel);
                }

                if (!reader.IsClosed)
                    reader.Close();

                return collection;
            }
            catch { throw; }
            finally { conn.Close(); }

        }

        public static void SendRequest(int menteeID, int mentorID)
        {
            SqlConnection conn = DataAccessor.GetConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_MatchMentorRequest]");
                cmd.Parameters.Add(new SqlParameter("@MenteeID", menteeID));
                cmd.Parameters.Add(new SqlParameter("@MentorID", mentorID));

                cmd.ExecuteNonQuery();
            }

            catch { throw; }
            finally { conn.Close(); }
            return;
        }

        public static void AcceptRequest(int menteeID, int mentorID)
        {
            SqlConnection conn = DataAccessor.GetConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_MatchMentorAccept]");
                cmd.Parameters.Add(new SqlParameter("@MenteeID", menteeID));
                cmd.Parameters.Add(new SqlParameter("@MentorID", mentorID));

                cmd.ExecuteNonQuery();
            }

            catch { throw; }
            finally { conn.Close(); }
            return;
        }

        public static void RejectRequest(int menteeID, int mentorID)
        {
            SqlConnection conn = DataAccessor.GetConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_MatchMentorReject]");
                cmd.Parameters.Add(new SqlParameter("@MenteeID", menteeID));
                cmd.Parameters.Add(new SqlParameter("@MentorID", mentorID));

                cmd.ExecuteNonQuery();
            }

            catch { throw; }
            finally { conn.Close(); }
            return;
        }

        public static List<MemaUser> GetListOfAcceptedMentorsByMentee(int menteeID)
        {
            SqlConnection conn = DataAccessor.GetConnection();
            IDataReader reader = null;
            List<MemaUser> collection = new List<MemaUser>();


            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_AcceptedMentorSelectByMentee]");
                cmd.Parameters.Add(new SqlParameter("@MenteeID", menteeID));
                cmd.ExecuteNonQuery();

                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    MemaUser dataModel = new MemaUser(reader);
                    collection.Add(dataModel);
                }

                if (!reader.IsClosed)
                    reader.Close();

                return collection;
            }
            catch { throw; }
            finally { conn.Close(); }

        }

        public static List<MemaUser> GetListOfAcceptedMenteesByMentor(int mentorID)
        {
            SqlConnection conn = DataAccessor.GetConnection();
            IDataReader reader = null;
            List<MemaUser> collection = new List<MemaUser>();


            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_AcceptedMenteesSelectByMentor]");
                cmd.Parameters.Add(new SqlParameter("@MentorID", mentorID));
                cmd.ExecuteNonQuery();

                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    MemaUser dataModel = new MemaUser(reader);
                    collection.Add(dataModel);
                }

                if (!reader.IsClosed)
                    reader.Close();

                return collection;
            }
            catch { throw; }
            finally { conn.Close(); }

        }

        public static List<MemaUser> GetListOfPendingRequestsByMentor(int mentorID)
        {
            SqlConnection conn = DataAccessor.GetConnection();
            IDataReader reader = null;
            List<MemaUser> collection = new List<MemaUser>();


            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_PendingMenteesSelectByMentor]");
                cmd.Parameters.Add(new SqlParameter("@MentorID", mentorID));
                cmd.ExecuteNonQuery();

                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    MemaUser dataModel = new MemaUser(reader);
                    collection.Add(dataModel);
                }

                if (!reader.IsClosed)
                    reader.Close();

                return collection;
            }
            catch { throw; }
            finally { conn.Close(); }

        }

        public static List<MemaUser> GetListOfPendingRequestsByMentee(int menteeID)
        {
            SqlConnection conn = DataAccessor.GetConnection();
            IDataReader reader = null;
            List<MemaUser> collection = new List<MemaUser>();


            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_PendingMentorSelectByMentee]");
                cmd.Parameters.Add(new SqlParameter("@MenteeID", menteeID));
                cmd.ExecuteNonQuery();

                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    MemaUser dataModel = new MemaUser(reader);
                    collection.Add(dataModel);
                }

                if (!reader.IsClosed)
                    reader.Close();

                return collection;
            }
            catch { throw; }
            finally { conn.Close(); }

        }
    }
}